package com.cssnj.portal.service.admin.rolefunction;

import com.cssnj.portal.domain.admin.GlJsGnEntity;
import com.cssnj.portal.domain.admin.GlJsGnRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.predictech.framework.domain.common.JsonRequest;
import io.predictech.framework.domain.common.JsonResponse;
import io.predictech.framework.service.base.BaseService;
import io.predictech.framework.domain.exception.BaseException;
import io.predictech.framework.domain.exception.BaseSysException;
import io.predictech.framework.utility.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("rolefunctionService")
public class RolefunctionService extends BaseService {


    @Autowired
    private GlJsGnRepository glJsGnRepository;

    @Override
    protected JsonResponse invokingService(JsonRequest jsonRequest) throws SQLException, BaseException {
        if ("init".equals(jsonRequest.getHandleCode())) {
            return init(jsonRequest);
        } else if ("saveData".equals(jsonRequest.getHandleCode())) {
            return saveData(jsonRequest);
        }
        return null;
    }

    //初始化
    private JsonResponse init(JsonRequest jsonRequest) throws SQLException, BaseSysException {
        JsonResponse jsonResponse = new JsonResponse();
        List<Map<String, Object>> jsList = new ArrayList<Map<String, Object>>();
        Map<String, Object> map;
        //查询有效角色
        List<Object> params = new ArrayList<Object>();
        params.add("1");
        String sql = "SELECT A.JS_ID ID,A.JS_MC TEXT FROM GL_JS A WHERE A.ZTBJ_DM = ?";
        SqlRowSet rs = this.queryDao.queryForRowSet(sql, params);
        int gs = 0;
        while (rs.next()) {
            map = new HashMap<String, Object>();
            map.put("id", rs.getString("id"));
            map.put("text", rs.getString("text"));
            map.put("state", "open");
            if (0 == gs) {
                map.put("checked", true);
                gs++;
            }
            jsList.add(map);
        }
        try {
            ObjectMapper objectMapper = StringUtil.getBaseObjectMapper();
            jsonResponse.getRepData().put("jsList", objectMapper.writeValueAsString(jsList));
        } catch (JsonProcessingException e) {
            throw new BaseSysException(e.getMessage());
        }
        return jsonResponse;
    }

    //保存数据
    @Transactional
    public JsonResponse saveData(JsonRequest jsonRequest) throws SQLException, BaseSysException {
        JsonResponse jsonResponse = new JsonResponse();
        //获取请求参数
        String jsid = (String) jsonRequest.getReqData().get("jsid");
        String str_gnlist = (String) jsonRequest.getReqData().get("str_gnlist");
        //获取当前人员
        String swrydm = (String) jsonRequest.getReqData().get("swrydm");
        //获取当前时间
        Timestamp lrsj = this.queryDao.getSystimestamp();
        //开始执行
        GlJsGnEntity vo;
        glJsGnRepository.deleteByJsId(jsid);
        if (!"".equals(str_gnlist)) {
            String[] gnArr = str_gnlist.split(",");
            for (int i = 0; i < gnArr.length; i++) {
                vo = new GlJsGnEntity();
                vo.setJsId(jsid);
                vo.setGnId(gnArr[i]);
                vo.setLrryId(swrydm);
                vo.setLrSj(lrsj);
                glJsGnRepository.save(vo);
            }
        }
        return jsonResponse;
    }
}
